import os
from win32com import client
from docx import Document
from docx.shared import Pt

doc_filename = "test.docx"     # 存放所有记录信息
doc_filepath = "F:/projects/adenoid_demo_new/" + doc_filename
#temp_filename = "temp.docx"     # 临时存放需要打印的记录
#temp_doc_filepath = "F:/projects/adenoid_demo_new/" + temp_filename

def create_word(filename, info):
    document = Document("F:/projects/adenoid_demo_new/template.docx")
    doc_filepath = "F:/projects/data/word/" + filename[:-4] + ".docx"
    print(doc_filepath)

    paragraph = document.add_paragraph()
    run = paragraph.add_run(filename[:-4])
    run.font.size = Pt(12)

    paragraph = document.add_paragraph()
    run = paragraph.add_run(info)
    run.font.size = Pt(12)

    paragraph = document.add_paragraph()
    run = paragraph.add_run("山东省公共卫生临床中心")
    run.font.size = Pt(10)

    paragraph = document.add_paragraph()
    run = paragraph.add_run("耳鼻咽喉科")
    run.font.size = Pt(10)

    paragraph = document.add_paragraph()
    run = paragraph.add_run("咨询电话：")
    run.font.size = Pt(10)

    paragraph = document.add_paragraph()
    run = paragraph.add_run("83347700")
    run.font.size = Pt(10)

    paragraph = document.add_paragraph()
    run = paragraph.add_run("83347235")
    run.font.size = Pt(10)


    document.save(doc_filepath)
    print_word(doc_filepath)

def file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        print(root)  # 当前目录路径
        print(dirs)  # 当前路径下所有子目录
        print(files)  # 当前路径下所有非目录子文件
        return files


def print_word(word_file_path):
    # 指示系统中文档的处理工具`
    # 指示运行的版本，如果是WPS应修改为
    # 如果使用word
    exec_tool = 'Word.Application'
    # 如果使用wps
    # exec_tool = 'wps.application'
    word = client.Dispatch(exec_tool)
    # 在后台运行程序
    word.Visible = 0  # 后台运行，不显示
    # 运行过程不警告
    word.DisplayAlerts = 0  # 不警告
    # 打开word文档
    doc = word.Documents.Open(word_file_path)
    # 进行打印
    doc.PrintOut()
    # --------------------------------------------------------
    # -- 最后操作保存
    # --------------------------------------------------------
    # 关闭文件
    doc.Close()
    # 退出word
    word.Quit()


if __name__ == '__main__': 
    create_word("你好，打印机！")
    # print_word(doc_filepath)
    
    
    